/*
 * 添加权限交易
 * 参数   交易码：trancode=1032,角色id：strjs，菜单id:arrcd
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TRights;
import org.ccams.util.StrHelper;
/**
 * 添加权限
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 * @时间：Aug 10, 2008
 */
public class Tran1032 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String strjs = (String) request.getParameter("strjs");
		String strcd = (String) request.getParameter("arrcd");
		strjs = (strjs != null) ? strjs : "";
		strcd = (strcd != null) ? strcd : "";
		String [] arrcd = StrHelper.split(strcd);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		getCcamsService().getTrightsDao().deleteTRights(strjs);//首先删除角色权限
		int k = 0;
		for(int i=0;i<arrcd.length;i++){//添加角色权限
			TRights t = new TRights();
			t.setRId(Long.parseLong(strjs));
			t.setMId(Long.parseLong(arrcd[i]));
			t.setValid(0);
			t.setAddDate(org.ccams.util.Utils.getCurDate());//获得当期时间
			k = getCcamsService().getTrightsDao().insertTRights(t);
		}
		
		if (k == 1) {
			success = true;
			message = "权限添加成功";
		} else {
			success = false;
			message = "权限添加失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
